package com.example.beikeyigou.pay;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.beikeyigou.R;
import com.example.beikeyigou.goods.GoodsTypeAdapter;
import com.example.beikeyigou.trolley.TrolleyBean;
import com.example.beikeyigou.utils.URLUtils;
import com.squareup.picasso.Picasso;

import java.util.List;

public class PayAdapter extends BaseAdapter {
    private Context context;
    private List<TrolleyBean.DataBean> data;


    public PayAdapter(Context context, List<TrolleyBean.DataBean> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        VH vh = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_pay_goods, parent, false);
            vh = new PayAdapter.VH(convertView);
            convertView.setTag(vh);
        } else {
            vh = (PayAdapter.VH) convertView.getTag();
        }
        TrolleyBean.DataBean dataBean = data.get(position);
        vh.goodTitleTv.setText(dataBean.getCommodityName());
        vh.priceTv.setText("￥" + dataBean.getTotalprices()/dataBean.getCount());
        vh.numTv.setText("×" + dataBean.getCount());
        String photo = dataBean.getCommodityPhoto();
        if (!TextUtils.isEmpty(photo)) {
            String photoUrl = URLUtils.PUBLIC_URL + photo;
            Picasso.with(context).load(photoUrl).into(vh.imageView);
        }

        return convertView;
    }

    class VH {
        private ImageView imageView;
        private TextView goodTitleTv, priceTv, numTv;

        public VH (View v) {
            imageView = v.findViewById(R.id.item_pay_img);
            goodTitleTv = v.findViewById(R.id.item_pay_title_tv);
            priceTv = v.findViewById(R.id.item_pay_price_tv);
            numTv = v.findViewById(R.id.item_pay_num_tv);
        }
    }
}
